// Script created 10.22.09 15:05:43
var PIC_ROOT = 'http://www.101stratford.com';

function mymouseMove(ev)
{
    var e = ev || window.event;
	var mousePos = mouseCoords(e);
        isover($('imageactual'),mousePos) ? 
            Controller.icursor.invoke(mousePos) : 
            ClearCursor();
	if (!mouseDatum) return false;
	var moveDatum  = {x:mousePos.x - mouseDatum.offset.x, y:mousePos.y - mouseDatum.offset.y};
    window.mouseMoveListener.invoke ({
        name     : 'mousemove', 
        position : moveDatum
    });
}

function ClearCursor()
{
    var d=['br','dmarquee'];
    for (var o=0;o<d.length;o++)
    if ($(d[o])) 
        $(d[o]).style.display='none';
}

function isover (obj, pt, cnt)
{
    if (!obj) return false;
    var pos = getPosition (obj);
    var me  = { x1:pos.x, x2:pos.x+obj.offsetWidth, y1:pos.y, y2:pos.y+obj.offsetHeight };
    return pt.x>me.x1&&pt.y>me.y1&&pt.x<me.x2&&pt.y<me.y2;
}


function onpimploaded ()
{
    if (! (app && app.name)) alert ('The application has not initialized properly');
    document.title      = app.name;   
    Controller.brush    = 1;
    Controller.color    = 'FFFF00';
    Controller.sstate   = 15;

    Controller.resizer  = new DHTMLEvent();
    Controller.scroller = new DHTMLEvent();
    Controller.icursor  = new DHTMLEvent();

    webservice          = PIMPConnector.create ();    
    AIM                 = AjaxUploader (webservice.endpoint);    

    webservice.ApplyToolCompleted = function ()
    {
        var dw=$('dwait');
        dw.style.display = 'none';
        RefreshImage();
    }
    document.onmousemove = mymouseMove;
}

function PleaseWait ()
{
   var dw=$('dwait');
   var htm='<table width="100%" height="100%" bgcolor=white><tr><td align="center">please wait...</td></tr></table>';
   dw.innerHTML = htm;
   dw.style.display = '';
   dw.style.position = 'absolute';
   dw.style.top="0px"
   dw.style.left="0px"
   dw.style.width="(0)px".format(window_w())
   dw.style.height="(0)px".format(window_h())
}

function onstart ()
{
  // alert ('onstart');
   window.onresize = function ()
   {
       Controller.resizer.invoke ();
    }
    AppConfig.gohome ();
}


function RestoreImage()
{
    var f=webservice.ResetImage (Controller.picture.id)
   // alert (f);
    Controller.EditPage.invoke ();
}


function SetSState(i)
{
    if (Controller.sstate & i)
      { Controller.sstate -= i } 
    else
      { Controller.sstate -=- i }
    Controller.UpdateSidebar.invoke();
}

function SetColor (color)
{
    Controller.color = color;
    //Controller.UpdateSidebar.invoke();
    LoadBrush (function () {Controller.UpdateSidebar.invoke()});
}

function SetBrush (brush)
{
    Controller.brush = brush;
    LoadBrush (function () {Controller.UpdateSidebar.invoke()});
   // Controller.UpdateSidebar.invoke();
}

var GetPictureById = function (id)
{
    var pic=Controller.pictures;
    for (var e=0;e<pic.length;e++)
         if (pic[e].id==id)
             return pic[e]
    return null;
}

var OpenPicture = function (id)
{
    var picture = GetPictureById(id);
    var size = picture.size.split (' ');
    Controller.picture = picture;
    Controller.info = { z:1, w:size[0], h:size[1] };
    Controller.EditPage.invoke();
}


function RefreshImage ()
{
   UpdatePicture.invoke();
}

function ClearResizer ()
{
    UpdatePicture.clear();
    Controller.scroller.clear ();
    Controller.resizer.clear ();
}

var UpdatePicture = {
    attach : function (name, pat, callback)
    {
        var pattern = pat, obj = $(name);
        if (!this.event) this.clear();
        obj.onload = callback;
        obj.invoke = function ()
        {
            this.src = pattern.format (Controller.picture.id, new Date().getTime())
        }
        this.event.subscribe (obj);
        this.invoke();
    },
    clear : function ()
    {
        this.event = new DHTMLEvent ();
    },
    event : null,
    invoke : function () { this.event.invoke() }
}

function AttachPreviewWindow()
{
    UpdatePicture.attach ('impreview', 'cache/(0).history.png?(1)', function () { Controller.scroller.invoke() });
    AttachThumbnailWindow ();
}

function AttachActualWindow()
{
    UpdatePicture.attach ('imageactual', 'working/(0)/(1)', function() { ClearBrush (); });
    var dm = $('dmarquee');
    dm.invoke = function ()
    {
        var iw  = $('imagewindow');
        var im  = $('imageactual');
        var ps1 = getPosition (im);
        this.style.border = "solid 2px lime";
        this.style.width  = im.offsetWidth;
        this.style.height = im.offsetHeight;
        this.style.left = "(0)px".format (ps1.x - iw.scrollLeft);
        this.style.top  = "(0)px".format (ps1.y - iw.scrollTop);
    }
    Controller.icursor.subscribe (dm);
}

function ResetImageSize(value)
{
    Controller.info.z = value;
    var im = $('imageactual');
    var iw = $('imagewindow');
    im.width  = Controller.info.z * Controller.info.w;
    im.height = Controller.info.z * Controller.info.h;

    if (iw)
    {
        iw.style.backgroundImage = 'url(http://www.101stratford.com/pimp/working/backgrid/(0))'.format (10 * Controller.info.z);
      //  alert (iw.style.backgroundImage);
    }
    else alert ('no iw')

    Controller.scroller.invoke ();
    MarqueeEngine.update();
}

function mylog (name)
{
  // document.title = '(0) (1)'.format(name, new Date().getTime())
}


function SwitchLogWindow (mode)
{ 
    PanelEngine.UI.Historylog.mode=mode;
    Controller.UpdateSidebar.invoke();
}


function AttachLogWindow ()
{ 
    webservice.GetPictureLogCompleted = function (sender, e)
    {
       var htm=[],log=e.Result,mode=PanelEngine.UI.Historylog.mode;
       var step = Controller.step ? Controller.step : log.length - 1;
       var ListviewModeOn = mode & PanelEngine.UI.Historylog.modes.LIST;
       htm.push ('<div align=center style="width:200px" class=imagewindow>');
       htm.push ('<img src="cache/(0).history.png" id="imlog" onerror=\'this.src="/webservices/images/clear.gif"\'">'.format(Controller.picture.id));
       htm.push ('</div>');
       htm.push ('<div align=right>');
       htm.push ('<a href="javascript:void( 0 )" onclick="SwitchLogWindow ( (1) )">Switch to (0) mode</a>'.format(
                  ListviewModeOn?'preview':'list',
                  ListviewModeOn?PanelEngine.UI.Historylog.modes.PREVIEW:PanelEngine.UI.Historylog.modes.LIST
                  ));
       htm.push ('</div>');
       if (ListviewModeOn)
       {
           for (var i=0;i<log.length;i++)
           {
              htm.push ('<div class="logentry"><img src="(0)" width="18"><a href="javascript:void( 0 )" onclick="ApplyPictureLog( (2) )">(1)</a></div>'.format(Tools[log[i].name].Icon, log[i].name, i))
           }
       }
       else
       { 
           var name = log[step].name;
           htm.push ('<div align=center>');
           htm.push ('(0) of (1)'.format(step + 1, log.length));
           htm.push ('</div>');
           htm.push ('<div class="logentry"><img src="(0)" width="18"><a href="javascript:void( 0 )" onclick="ApplyPictureLog( (2) )">(1)</a></div>'.format(Tools[name].Icon, name, step))
       }
        $('dlog').innerHTML = htm.join('');
    }
    if ($('dlog')) 
    {
        $('dlog').innerHTML = 'loading history...'
        webservice.GetPictureLogAsync(Controller.picture.id)
    }
}


function ApplyPictureLog(step)
{
    Controller.step = step;
    webservice.ApplyPictureLog (Controller.picture.id, step);
    $('imlog').src = "cache/(0).history.png?(1)".format(Controller.picture.id,new Date().getTime());
}

